<?php
/**
 * @author Nguyen Thanh Tung
 * @copyright 2014
 * @description: save action sample
 */
// not login
// not perm add
if( !isset($AppUI) ||
	!isset($_SESSION['stadium']) ||
	(isset($_SESSION['stadium']) && ($_SESSION['stadium']&1<<2)==0)	
	/*|| 
	(isset($AppUI->id) && !in_array($AppUI->type, array(ROLE_ROOT, ROLE_ADMINISTRATOR)))*/
) {
	redirect(HOST_BACKEND);
}
// data input
$intClubId	             = (int)$this->input->post('clubsid',0);
$strStadiumname          = $this->input->post('pname',null); 
$intStadiumId			 = (int)$this->input->post('stadiumid',0);//test($intStadiumId);
$strStadiumnShort        = $this->input->post('pshort',null);			
$strStadiumDescription   = $this->input->post('pdescription',null);
$strStadiumOpenDate      = $this->input->post('popen', null);
$strStadiumCloseDate     = $this->input->post('pclosed', null);
$intActive				 = $this->input->post('pactive',1);
$intCapacity			 = $this->input->post('capacity',1);
$arrErr                  = array();

// strip tag data input
$strStadiumname        = CI_Function::stripTag($strStadiumname);
$lenStadiumname        = strlen($strStadiumname);

$strStadiumnShort        = CI_Function::stripTag($strStadiumnShort);
$lenStadiumnShort        = strlen($strStadiumnShort);

$strStadiumDescription   = CI_Function::stripTag($strStadiumDescription);
$lenStadiumDescription   = strlen($strStadiumDescription);
// validate manual	
//+ name	
if(!isset($strStadiumname) || empty($strStadiumname))
    array_push($arrErr, $arrLocale['name_required']);
elseif(isset($strStadiumname) && ($lenStadiumname < VL_NAME_MINLENGTH   || $lenStadiumname > VL_NAME_STADIUM_MAXLENGTH) ) 
    array_push($arrErr, $arrLocale['name_range']);   
//+ short	
if(!isset($strStadiumnShort) || empty($strStadiumnShort))
    array_push($arrErr, $arrLocale['short_required']);
elseif(isset($strStadiumnShort) && ($lenStadiumnShort < VL_NAME_MINLENGTH || $lenStadiumnShort > VL_NAME_STADIUM_MAXLENGTH) ) 
    array_push($arrErr, $arrLocale['short_range']);
//+ description	
if(!isset($strStadiumDescription) || empty($strStadiumDescription))
    array_push($arrErr, $arrLocale['description_required']);
elseif(isset($strStadiumDescription) && ($lenStadiumDescription < VL_CONTENT_MINLENGTH) ) 
    array_push($arrErr, $arrLocale['description_minlength']);  

	 
if(isset($arrErr) && count($arrErr)==0) {//allow save
    // unset session if exists
	
    if(isset($_SESSION['STADIUM_ERROR'])) unset($_SESSION['STADIUM_ERROR']);
    if(isset($_SESSION['STADIUM_POST'])) unset($_SESSION['STADIUM_POST']);
    $this->load->model('stadium_model', 'stadium');	
	  
    $intError = $this->stadium->update($AppUI->id,$intStadiumId,$intClubId, $strStadiumname, $strStadiumnShort, $strStadiumDescription, $intActive,$intCapacity , CI_Function::convertStrToDate($strStadiumOpenDate),CI_Function::convertStrToDate($strStadiumCloseDate));
    if($intError==0) CI_Function::setMsg($arrMessage['save_update']);
    elseif($intError==1) array_push($arrErr, $arrError['noperm_save']);
}
// occur error

if (isset($arrErr) && count($arrErr) > 0) {
    $_SESSION['STADIUM_ERROR']    = $arrErr;    
    $_SESSION['STADIUM_POST']     = $this->input->post();
    
    redirect(HOST_BACKEND . 'stadium/edit?stadiumid='.$intStadiumId);
}
redirect(HOST_BACKEND.'stadium/index?src=0&id='.$intClubId);
